package wj.tree;

import java.util.ArrayList;

import wj.WJValue;

public class BlockNode implements WJNode{

	private ArrayList<WJNode> statements;
	private WJNode returnNode;
	
	public BlockNode(){
		statements = new ArrayList<WJNode>();
	}
	
	@Override
	public WJValue evaluate() {
		for(WJNode statement: statements){
			WJValue returnVal = statement.evaluate();
			
			if(statement instanceof IfStatementNode || statement instanceof WhileNode || statement instanceof BlockNode)
				if(!returnVal.equals(WJValue.NULL))
					return returnVal;
		}
		
		if(returnNode == null)
			return WJValue.NULL;
		
		return returnNode.evaluate();
	}
	
	public void addStatement(WJNode statement){
		if(statement != null)
			statements.add(statement);
	}
	
	public void setReturn(WJNode returnNode){
		this.returnNode = returnNode;
	}

}
